package sm.upgrade;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.upgrade.api.UpgradePhase;
import sm.util.SMMap;

public class SvcCatalogDetailedDescUpgrade implements UpgradePhase {
	private static Logger logger = LoggerFactory.getLogger(UpgradePhase.class);

	private HibernateTemplate template;

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

	@Override
	public void upgrade() {
		logger.info("Updating detailedDesc field in the svcCatalog table...");

		List<Object> svcCatalogs = template.find(
				"from svcCatalog where type != ?", "category");
		if (svcCatalogs != null) {
			for (Object svcCatalog : svcCatalogs) {
				SMMap svcCatalogMap = (SMMap) svcCatalog;
				String strDesc = (String) svcCatalogMap.get("description");
				svcCatalogMap.put("detailedDescription",
						strDesc.replaceAll("\\n", "<br>"));
				template.update("svcCatalog", svcCatalogMap);
			}
		}

		logger.info("  Finished updating detailedDesc field in the svcCatalog table.");

	}

}
